﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RapChieuPhim.DTO;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using RapChieuPhim.BUS;

namespace RapChieuPhim.DAO
{
    class CTTheLoaiDAO
    {
        public CTTheLoaiDTO GetByID(int theloai, int doituong)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@MaTL", theloai);
            param[1] = new SqlParameter("@MaDoiTuong", doituong);
            DataTable dt = cn.SqlExcuteQuery("LayCTTheLoaiTheoID", param);
            CTTheLoaiDTO CTTheLoai = new CTTheLoaiDTO();

            if (dt.Rows.Count != 0)
            {
                CTTheLoai.TheLoai = TheLoaiBUS.GetByID(int.Parse(dt.Rows[0]["MaTL"].ToString()));
                CTTheLoai.DoiTuong= DoiTuongBUS.GetByID(int.Parse(dt.Rows[0]["MaDoiTuong"].ToString()));
            }
            return CTTheLoai;
        }

        public IList GetAll()
        {
            DataProvider cn = new DataProvider();
            ArrayList array = new ArrayList();
            DataTable dt = cn.SqlExcuteQuery("LayAllCTTheLoai");

            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    CTTheLoaiDTO CTTheLoai = new CTTheLoaiDTO();
                    CTTheLoai.TheLoai = TheLoaiBUS.GetByID(int.Parse(dr["MaTL"].ToString()));
                    CTTheLoai.DoiTuong = DoiTuongBUS.GetByID(int.Parse(dr["MaDoiTuong"].ToString()));
                    array.Add(CTTheLoai);
                }
            }
            return array;
        }

        public void Insert(CTTheLoaiDTO CTTheLoai)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@MaTL", CTTheLoai.TheLoai);
            param[1] = new SqlParameter("@MaDoiTuong", CTTheLoai.DoiTuong);
            cn.SqlExcuteNonQuery("InsertCTTheLoai", param);
        }

        public void Update(CTTheLoaiDTO CTTheLoai)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@MaTL", CTTheLoai.TheLoai);
            param[1] = new SqlParameter("@MaDoiTuong", CTTheLoai.DoiTuong);
            cn.SqlExcuteNonQuery("UpdateCTTheLoai", param);
        }

        public void Delete(int theloai, int doituong)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@MaTL", theloai);
            param[1] = new SqlParameter("@MaDoiTuong", doituong);
            cn.SqlExcuteNonQuery("DeleteCTTheLoai", param);
        } 
    }
}
